package com.ithiema.edu.info.manager.controller;

import com.ithiema.edu.info.manager.domain.Student;
import com.ithiema.edu.info.manager.service.StudentService;
import java.util.Scanner;

public abstract class BaseStudentController {
    //创建键盘输入类对象
    Scanner sc = new Scanner(System.in);
    //创建StudentService类对象
    private StudentService studentService = new StudentService();

    public void start() {
        loop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    //System.out.println("添加");
                    addStudent();
                    break;
                case 2:
                    //System.out.println("删除");
                    deleteStudent();
                    break;
                case 3:
                    //System.out.println("修改");
                    reviseStudent();
                    break;
                case 4:
                    //System.out.println("查看");
                    showStudent();
                    break;
                case 5:
                    System.out.println("退出");
                    break loop;
                default:
                    System.out.println("您输入有误,请重新输入");
            }
        }
    }

    //删除学生类
    private void deleteStudent() {
        String id = input_StudentId("删除");
        //删除
        studentService.deleteStudent(id);
        System.out.println("删除成功");
    }

    //修改学员信息
    private void reviseStudent() {
        String id = input_StudentId("修改");
        //输入学员信息
        Student stu = input_Student(id);
        //调用StudentService类reviseStudent方法，修改学员信息
        studentService.reviseStudent(id, stu);
        System.out.println("修改信息成功");
    }

    //查看学员信息
    private void showStudent() {
        studentService.ShowStudent();
    }

    //添加学员信息方法
    public void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学号");
            id = sc.next();
            //判断学生数组里是否有相同学号
            boolean id_flag = studentService.seekIndex(id);
            if (!id_flag) {
                break;
            }
            System.out.println("学号已被占用");
        }
        //输入学员信息
        Student stu = input_Student(id);
        //调用StudentService类addStudent方法，添加学员
        boolean flag = studentService.addStudent(stu);
        //判断是否添加成功
        if (flag) {
            System.out.println("添加信息成功");
        } else {
            System.out.println("添加信息失败");
        }
    }

    //简化学员学号输入
    private String input_StudentId(String s) {
        String id;
        System.out.println("请输入要" + s + "信息的学员学号");
        while (true) {
            id = sc.next();
            //判断学生数组里是否有相同学号
            boolean id_flag = studentService.seekIndex(id);
            if (id_flag) {
                System.out.println("已找到该学员信息");
                break;
            }
            System.out.println("学号输入有误，请重新输入");
        }
        return id;
    }

    //简化学员信息输入
    abstract Student input_Student(String id);
}
